<template>
  <div class="app-container">
    <div
      style="width: 150px; position: fixed; top: 70px; left: 20px"
      v-if="statusList.length > 0"
    >
      <div style="display: flex; align-items: center">
        <el-button size="mini" type="text" icon="el-icon-plus" @click="deptAdd"
          >添加</el-button
        >
        <el-button
          style="color: #67c23a"
          size="mini"
          :disabled="!statuIndex"
          type="text"
          icon="el-icon-edit"
          @click="deptEdit"
          >修改</el-button
        >
        <el-button
          size="mini"
          :disabled="!statuIndex"
          type="text"
          icon="el-icon-delete"
          @click="deptDel"
          style="color: #f56c6c"
          >删除</el-button
        >
      </div>
      <div
        style="
          height: calc(100vh - 108px);
          background: #f4faff;
          display: flex;
          flex-direction: column;
          align-items: center;
          overflow: auto;
          margin-top: 10px;
          padding: 10px 0;
          box-sizing: border-box;
        "
      >
        <div
          style="margin-bottom: 10px"
          v-for="(item, index) in statusList"
          :key="index"
        >
          <el-button
            style="width: 120px"
            size="mini"
            @click="changeStatus(item)"
            :type="statuIndex === item.id ? 'primary' : ''"
            >{{ item.name }}</el-button
          >
        </div>
      </div>
    </div>
    <div v-else style="width: 150px; position: fixed; top: 70px; left: 20px">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="deptAdd"
        >添加部门</el-button
      >
    </div>
    <div style="margin-left: 160px">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="报销人" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入报销人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            >删除</el-button
          >
        </el-col>
      </el-row>
      <el-table
        ref="multipleTable"
        style="margin-top: 20px"
        v-loading="loading"
        :data="companyList"
        @selection-change="handleSelectionChange"
        @row-click="handleCurrentChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="报销人" align="center" prop="name" />
        <el-table-column label="所属部门" align="center" prop="deptName" />
        <el-table-column label="创建日期" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加或修改商家管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="报销人" prop="name">
          <el-input v-model="form.name" placeholder="请输入报销人" />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <el-select
            style="width: 100%"
            v-model="form.deptId"
            clearable
            placeholder="请选择所属部门"
          >
            <el-option
              v-for="item in statusList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加部门 -->
    <el-dialog
      :title="title"
      :visible.sync="deptShow"
      width="700px"
      append-to-body
    >
      <el-form
        ref="deptform"
        :model="deptform"
        :rules="deptrules"
        label-width="100px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="deptform.name" placeholder="请输入部门名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDept">确 定</el-button>
        <el-button @click="deptShow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAllInvoiceDept,
  getInvoiceDept,
  delInvoiceDept,
  addInvoiceDept,
  updateInvoiceDept,
} from "@/api/business/invoiceDept";

import {
  listAllInvoiceExpensesUser,
  getInvoiceExpensesUser,
  delInvoiceExpensesUser,
  addInvoiceExpensesUser,
  updateInvoiceExpensesUser,
} from "@/api/business/invoiceExpensesUser";

export default {
  name: "log",
  data() {
    return {
      deptName: "",
      deptform: {},
      deptrules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
        ],
      },
      deptShow: false,
      statuIndex: null,
      statusList: [],
      companyAllList: [],
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家管理表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      selectId: null,
      queryParams: {
        name: null,
        deptId: null,
        parentDeptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "报销人不能为空", trigger: "blur" }],
        deptId: [
          { required: true, message: "所属部门不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.selectId = JSON.parse(localStorage.getItem("selectCompany")).deptId;
    this.queryParams.parentDeptId = this.selectId;
    this.getList();
    this.getAllList();
  },
  methods: {
    changeStatus(item) {
      this.statuIndex = item.id;
      this.deptName = item.name;
      this.queryParams.deptId = item.id;
      this.getList();
    },
    deptAdd() {
      this.deptReset();
      this.title = "添加部门";
      this.deptShow = true;
    },
    /** 查询商家管理列表 */
    getList() {
      this.loading = true;
      listAllInvoiceExpensesUser(this.queryParams).then((response) => {
        this.companyList = response.data;
        this.loading = false;
      });
    },
    //全部厂商
    getAllList() {
      listAllInvoiceDept({ deptId: this.selectId }).then((response) => {
        this.statusList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        deptId: null,
        parentDeptId: this.selectId,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.statuIndex = null;
      this.queryParams.deptId = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleCurrentChange(val) {
      this.$refs.multipleTable.toggleRowSelection(val);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报销人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInvoiceExpensesUser(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报销人";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInvoiceExpensesUser(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoiceExpensesUser(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitDept() {
      this.$refs["deptform"].validate((valid) => {
        if (valid) {
          if (this.deptform.id != null) {
            updateInvoiceDept(this.deptform).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.deptShow = false;
              this.getAllList();
            });
          } else {
            addInvoiceDept(this.deptform).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.deptShow = false;
              this.getAllList();
            });
          }
        }
      });
    },
    deptReset() {
      this.deptform = {
        id: null,
        name: null,
        deptId: this.selectId,
      };
      this.resetForm("deptform");
    },
    deptEdit() {
      this.deptReset();
      getInvoiceDept(this.statuIndex).then((response) => {
        this.deptform.id = response.data.id;
        this.deptform.name = response.data.name;
        this.deptShow = true;
        this.title = "修改部门";
      });
    },
    deptDel() {
      console.log(this.statuIndex);

      this.$modal
        .confirm("是否确认删除该部门？")
        .then(() => {
          return delInvoiceDept(this.statuIndex);
        })
        .then(() => {
          this.getAllList();
          this.$modal.msgSuccess("删除成功");
          this.statuIndex = null;
          this.getList();
        })
        .catch((e) => {
          console.log(e);
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该报销人？")
        .then(function () {
          return delInvoiceExpensesUser(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/company/export",
        {
          ...this.queryParams,
        },
        `company_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.app-container {
  padding: 20px 30px;
  box-sizing: border-box;
}
</style>
